package larrys.pomodoro.domain.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;

/**
 * User. Note that, the default value is a valid user. that is:<br>
 * <code>
 * <table border="1" cellspacing="0">
 * <tr>
 * <td>expired</td>
 * <td rowspan="3">false</td>
 * </tr>
 * <tr>
 * <td>locked</td>
 * </tr>
 * <tr>
 * <td>credentialsExpired</td>
 * </tr>
 * <tr>
 * <td>enabled</td>
 * <td>true</td>
 * </tr>
 * </table>
 * 
 * @author larry.li
 * 
 */
public class PomodoroUser implements Serializable {

	private static final long serialVersionUID = 1L;

	private long id;
	private String email;
	private String username;
	private String password;
	private boolean male = true;
	private boolean expired = false;
	private boolean locked = false;
	private boolean credentialsExpired = false;
	private boolean enabled = true;

	private Collection<Role> roles = new HashSet<Role>();

	public PomodoroUser(long id, String email) {
		this.id = id;
		this.email = email;
	}

	public long getId() {
		return id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public Collection<Role> getRoles() {
		return roles;
	}

	public void addRole(Role role) {
		this.roles.add(role);
	}

	public void setRoles(Collection<Role> role) {
		this.roles = role;
	}

	public String getEmail() {
		return email;
	}

	public boolean isMale() {
		return male;
	}

	public void setMale(boolean male) {
		this.male = male;
	}

	public boolean isExpired() {
		return expired;
	}

	public void setExpired(boolean expired) {
		this.expired = expired;
	}

	public boolean isLocked() {
		return locked;
	}

	public void setLocked(boolean locked) {
		this.locked = locked;
	}

	public boolean isCredentialsExpired() {
		return credentialsExpired;
	}

	public void setCredentialsExpired(boolean credentialsExpired) {
		this.credentialsExpired = credentialsExpired;
	}

	public PomodoroUser cloneUserWithIdAndEmail(long id, String email) {
		PomodoroUser clone = new PomodoroUser(id, email);
		clone.setUsername(username);
		clone.setPassword(password);
		clone.setMale(male);
		clone.setExpired(expired);
		clone.setLocked(locked);
		clone.setCredentialsExpired(credentialsExpired);
		clone.setEnabled(enabled);
		clone.setRoles(roles);
		return clone;
	}

	@Override
	public String toString() {
		return "PomodoroUser [id=" + id + ", email=" + email + ", username="
				+ username + ", password=" + password + ", male=" + male
				+ ", expired=" + expired + ", locked=" + locked
				+ ", credentialsExpired=" + credentialsExpired + ", enabled="
				+ enabled + ", role=" + roles + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (credentialsExpired ? 1231 : 1237);
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + (enabled ? 1231 : 1237);
		result = prime * result + (expired ? 1231 : 1237);
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + (locked ? 1231 : 1237);
		result = prime * result + (male ? 1231 : 1237);
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((roles == null) ? 0 : roles.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PomodoroUser other = (PomodoroUser) obj;
		if (credentialsExpired != other.credentialsExpired)
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (enabled != other.enabled)
			return false;
		if (expired != other.expired)
			return false;
		if (id != other.id)
			return false;
		if (locked != other.locked)
			return false;
		if (male != other.male)
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (roles == null) {
			if (other.roles != null)
				return false;
		} else if (!roles.equals(other.roles))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}

}
